home *** CD-ROM | disk | FTP | other *** search
/ Freelog 117 / FreelogNo117-OctobreNovembre2013.iso / Programmation / jedit / jedit5.1.0install.exe / {app} / macros / Editing / Greedy_Delete.bsh < prev    next >
Text File  |  2013-07-28  |  2KB  |  88 lines

  1. /*
  2.  * Greedy_Delete.bsh - If a buffer is using soft tabs,
  3.  * this macro will delete tabSize number of spaces, if
  4.  * all the characters between the caret and the next tab
  5.  * stop are spaces.  In all other cases a single character
  6.  * is deleted.
  7.  *
  8.  * Copyright (C) 2004 Ollie Rutherfurd <oliver@jedit.org>
  9.  *
  10.  * $Id: Greedy_Delete.bsh 5230 2005-07-20 13:31:08Z orutherfurd $
  11.  */
  12.  
  13. /**
  14.  * @param onlyFullTabs if true, multiple spaces are only
  15.  *                     removed if they would constitute
  16.  *                     a 'complete' tab.
  17.  */
  18. void greedyDelete(View view, boolean onlyFullTabs)
  19. {
  20.     JEditTextArea textArea = view.getTextArea();
  21.     JEditBuffer buffer = textArea.getBuffer();
  22.     int caret = textArea.getCaretPosition();
  23.     int caretLine = textArea.getCaretLine();
  24.     int lineStart = textArea.getLineStartOffset(caretLine);
  25.     int lineEnd = textArea.getLineEndOffset(caretLine);
  26.  
  27.     if(buffer.getBooleanProperty("noTabs") == true)
  28.     {
  29.         // if anything is selected, use standard 
  30.         if(textArea.getSelection().length != 0)
  31.         {
  32.             textArea.delete();
  33.         }
  34.         // if at the end of the line, go to the 
  35.         // start of the next line (+1 for \n)
  36.         else if(caret+1 >= lineEnd)
  37.         {
  38.             textArea.delete();
  39.         }
  40.         else
  41.         {
  42.             int col = caret - lineStart;
  43.             int tabSize = buffer.getIntegerProperty("tabSize",8);
  44.  
  45.             // unlikely, but just in case
  46.             if(tabSize <= 0)
  47.             {
  48.                 textArea.delete();
  49.             }
  50.             else
  51.             {
  52.                 int toTabStop = (((col+tabSize)-1) % tabSize) + 1;
  53.                 // don't wrap to next line
  54.                 if(caret+toTabStop > lineEnd){
  55.                     textArea.delete();
  56.                     return;
  57.                 }
  58.  
  59.                 int count = 0;
  60.                 for(int i=0; i < toTabStop; i++)
  61.                 {
  62.                     if(!" ".equals(buffer.getText(caret+i,1)))
  63.                         break;
  64.                     count += 1;
  65.                 }
  66.  
  67.                 // if onlyFullTabs must be only spaces to
  68.                 // the tabStop and must have tabSize number 
  69.                 // of spaces to remove them all.
  70.                 if(onlyFullTabs == false || count == tabSize){
  71.                     buffer.remove(caret,count);
  72.                 }
  73.                 else{
  74.                     textArea.delete();
  75.                 }
  76.             }
  77.         }
  78.     }
  79.     else
  80.         textArea.delete();
  81. }
  82.  
  83. if(buffer.isReadOnly())
  84.     Toolkit.getDefaultToolkit().beep();
  85. else
  86.     greedyDelete(view,true);
  87.  
  88.